﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProtoBuf;

using Step4.Unit3.SystemMiddleWare.Utils;
using System.Data;
using System.Net.NetworkInformation;

namespace Step4.Unit3.SystemMiddleWare.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        // 项目启动的时候，系统会自动注册IHostEnvironment接口到IOC容器
        private readonly IWebHostEnvironment _webHostEnvironment; // IHostEnvironment 接口，字面上意思是主机环境，就是应用实例运行环境。其中包含"根目录路径"等一系列信息。
        private readonly IHostEnvironment _hostEnvironment;
        private readonly ISession _session; // 注入ISession服务
        private readonly IHttpContextAccessor _httpContextAccessor;

        /// </summary>
        /// <param name="logger"></param>
        /// <param name="hostEnvironment"></param>
        /// <param name="hostEnvironment1"></param>
        /// <param name="session"></param>

        /*
           IWebHostEnvironment 是 IHostEnvironment 的子接口，
           包含的信息比较多，
           所以只适用IWebHostEnvironment
        */

        public HomeController(  
            ILogger<HomeController> logger,
            IWebHostEnvironment hostEnvironment,
            IHostEnvironment hostEnvironment1,
            IHttpContextAccessor httpContextAccessor)
        {
            _logger= logger;
            _webHostEnvironment= hostEnvironment;
            _hostEnvironment = hostEnvironment;
            _httpContextAccessor = httpContextAccessor;
            _session = _httpContextAccessor.HttpContext.Session;
        }

        public IActionResult Index()
        {
            _logger.LogInformation("WebRootPath："+_webHostEnvironment.WebRootPath);
            _logger.LogInformation("ContentRootPath：" + _webHostEnvironment.ContentRootPath);

            /*
                 每个用户都有一个Seesion,
                 所以每个用户都在服务器上拥有一个内存块，
                 用户不多的系统还行，如果用途特别多，在线量大的项目是不适合使用Session的
            */
            _session.SetString("username","李大嘴"); // Session可以在控制器间进行数据交换
            return View();
        }

        public IActionResult SetUser()
        {
            UserInfo info = new(1,"任我行",Convert.ToDateTime("1999-1-1"));

            // 对象序列化
            // 将对象序列化后，放到session中
            // _session.SetString("user", JsonConvert.SerializeObject(info));
            // 使用SessionUitl扩展后的session
            _session.Set("user", info);  // 到 Utils/SessionUtil.cs 看看如何扩展一个类，或者一个接口的

            return Ok("添加成功了");
        }

        public IActionResult GetUser()
        { 
            //var user = _session.GetString("user").ToString();
            //var userInfo = JsonConvert.DeserializeObject<UserInfo>(user);
            // 使用扩展
            return Ok(_session.Get<UserInfo>("user"));
        }


        /// <summary>
        /// 生产环境异常页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Error()
        {
            return View();
        }
    }


    [ProtoContract] // 协议合同属性
    public class UserInfo
    {
        public UserInfo() { 
        
        }

        public UserInfo(int id, string nickName, DateTime birthday)
        { 
            Id = id;
            NickName = nickName;
            Birthday = birthday;
        }

        [ProtoMember(1)] // 参数为属性被序列化的顺序
        public int Id { get; set; }
        [ProtoMember(2)]
        public string NickName { get; set; }
        [ProtoMember(3)]
        public DateTime Birthday { get; set; }  
    }
}
